﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace zielonka.co.uk.extensions.system.data
{
    // Description: Get column value bu name from IDataReader.
    /*
        var value = rdr.GetValue<string>(columnName);
        var value = rdr.GetValue(columnName);
        var value = rdr.GetValue<int>(columnName);
        var value = rdr.GetValue<int?>(columnName);
    */


    // A set of extension methods for IDataReader
    public static partial class DataReaderExtensions
    {
        public static string GetValue(this IDataReader rdr, string columnName)
        {
            return rdr.GetValue<string>(columnName);
        }

        public static T GetValue<T>(this IDataReader rdr, string columnName)
        {
            object data = rdr.GetValue(rdr.GetOrdinal(columnName));
            if (!string.IsNullOrEmpty(data.ToString()))
                return (T)data;

            return default(T);
        }
    }
}
